package com.maktoub.project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maktoub.common.model.entity.InterfaceInfo;
import com.maktoub.project.common.ErrorCode;
import com.maktoub.project.exception.BusinessException;
import com.maktoub.project.mapper.InterfaceInfoMapper;
import com.maktoub.project.service.InterfaceInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author Maktoub
 * @description 针对表【interface_info(接口信息)】的数据库操作Service实现
 * @createDate 2024-05-07 09:34:49
 */
@Service
public class InterfaceInfoServiceImpl extends ServiceImpl<InterfaceInfoMapper, InterfaceInfo>
        implements InterfaceInfoService {

    public void validInterfaceInfo(InterfaceInfo interfaceInfo, boolean add) {


        if (interfaceInfo == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long id = interfaceInfo.getId();
        String name = interfaceInfo.getName();
        String description = interfaceInfo.getDescription();
        String url = interfaceInfo.getUrl();
        String requestParams = interfaceInfo.getRequestParams();
        String requestHeader = interfaceInfo.getRequestHeader();
        String responseHeader = interfaceInfo.getResponseHeader();
        String method = interfaceInfo.getMethod();

        //todo userID


        if (add) {
            if (StringUtils.isAnyBlank(name, url, description, requestHeader, requestParams, responseHeader, method)) {
                    throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数不能为空");
            }
        }
        else{
            if(id<0){
             throw new BusinessException(ErrorCode.PARAMS_ERROR) ;
            }
        }
        if (StringUtils.isNoneBlank(name) && name.length() > 30) {

            throw new BusinessException(ErrorCode.PARAMS_ERROR, "接口名称过长");
        }
        ArrayList<String> strings = new ArrayList<>(Arrays.asList("get", "put", "delete", "post"));
        if (StringUtils.isNotBlank(method) && !strings.contains(method.toLowerCase())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求方法仅限GET,POST,PUT,DELETE");
        }
        if (StringUtils.isNoneBlank(description) && description.length() > 120) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "接口描述过长");
        }

        //请求地址
        if (StringUtils.isNoneBlank(url) && url.length() > 250) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "url过长");
        }

    }
}





